﻿//准备ExtJS环境
Ext.require
(
    [
        'Ext.grid.*',
        'Ext.data.*',
        'Ext.util.*',
        'Ext.state.*',
        'Ext.form.*',
        'Ext.tree.*'
    ]
);

//定义用户表格控件
Ext.define
(
    'Tree.Grid',
    {
        extend: 'Ext.tree.Panel',

        //检索窗体
        win_search: null,

        //编辑窗体
        win_edit: null,

        //新增窗体
        win_create: null,

        //初始化组件
        initComponent: function ()
        {
            Ext.apply
            (
                this,
                {
                    listeners:
                    {
                        //设置两个指头的滚动
                        render: Common.ScrollableRender
                    },
                    dockedItems:
                    [
                        {
                            xtype: 'toolbar',
                            height: 36,
                            items:
                            [
                                {
                                    iconCls: 'icon-refresh',
                                    text: '刷新',
                                    scope: this,
                                    hidden: false,
                                    tooltip: '刷新数据',
                                    handler: this.onRefreshClick
                                },
                                '-',
                                {
                                    iconCls: 'icon-add',
                                    text: '新增',
                                    scope: this,
                                    hidden: false,
                                    itemId: 'tree-add',
                                    disabled: true,
                                    tooltip: '新增组织结构',
                                    handler: this.onAddClick
                                },
                                {
                                    iconCls: 'icon-delete',
                                    text: '删除',
                                    disabled: true,
                                    hidden: false,
                                    itemId: 'tree-delete',
                                    scope: this,
                                    tooltip: '删除组织结构',
                                    handler: this.onDeleteClick
                                },
                                {
                                    iconCls: 'icon-edit',
                                    text: '编辑',
                                    itemId: 'tree-edit',
                                    disabled: true,
                                    hidden: false,
                                    scope: this,
                                    tooltip: '编辑组织结构',
                                    handler: this.onEditClick
                                }
                            ]
                        }
                    ]
                }
            );
            this.callParent();
            this.getSelectionModel().on('selectionchange', this.onSelectionChange, this);
        },

        //处理选中项目变化事件
        onSelectionChange: function (selectionModel, selections)
        {
            //判断是否选中项目
            if (selections.length === 0)
            {
                //禁用新增按钮
                this.down('#tree-add').setDisabled(true);
                //禁用删除按钮
                this.down('#tree-delete').setDisabled(true);
                //禁用编辑按钮
                this.down('#tree-edit').setDisabled(true);
            }
            else
            {
                //获得选中的行
                var selection = this.getView().getSelectionModel().getSelection()[0];

                //启用删除按钮
                this.down('#tree-delete').setDisabled(!selection.data.leaf);
                //启用新增按钮
                this.down('#tree-add').setDisabled(false);
                //启用编辑按钮
                this.down('#tree-edit').setDisabled(selection.data.root);
            }
        },

        //处理删除按钮点击事件
        onDeleteClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection()[0];

            //判断是否选中行
            if (selection)
            {
                //询问是否确定删除
                Ext.Msg.show
                (
                    {
                        title: '删除组织结构',
                        width: 320,
                        height: 240,
                        msg: '确定要删除选中的组织结构"' + selection.data.name + '"吗?',
                        buttons: Ext.Msg.YESNO,
                        icon: Ext.Msg.QUESTION,
                        fn: function (button)
                        {
                            //判断是否确定删除 
                            if (button == 'yes')
                            {
                                //创建请求对象
                                Ext.Ajax.request
                                (
                                    {
                                        url: Common.MakeAbsoluteUrl('Delete'),
                                        method: HttpMethod.Post,
                                        params: selection.data,
                                        callback: function (options, success, response)
                                        {
                                            //处理错误
                                            if (Common.HandleError(options, success, response))
                                            {
                                                //刷新数据
                                                store.load();
                                            }
                                        }
                                    }
                                );
                            }
                        }
                    }
                );
            }
        },

        //处理刷新按钮点击事件
        onRefreshClick: function ()
        {
            //刷新数据
            this.store.load();
        },

        //处理新增按钮点击事件
        onAddClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection()[0];

            //判断是否选中行
            if (selection)
            {
                //判定窗体是否创建
                if (!this.win_create)
                {
                    //创建窗体
                    this.win_create = new Ext.Window
                    (
                        {
                            title: '新增组织结构',
                            maximizable: false,
                            maximized: false,
                            width: 360,
                            height: 240,
                            bodyBorder: false,
                            draggable: true,
                            isTopContainer: true,
                            modal: true,
                            resizable: false,
                            bodyPadding: 0,
                            closeAction: 'hide',
                            closable: false,
                            layout: 'fit',
                            fieldDefaults:
                            {
                                labelAlign: 'left',
                                labelWidth: 72,
                                anchor: '100%'
                            },
                            listeners:
                            {
                                show: function ()
                                {
                                    //设置焦点
                                    this.down('field').focus(false, 50);
                                }
                            },
                            items:
                            [
                                {
                                    xtype: 'form',
                                    method: HttpMethod.Post,
                                    waitMsgTarget: true,
                                    waitTitle: Common.ApplicationName,
                                    frame: false,
                                    bodyPadding: 12,
                                    bodyStyle: 'background-color:White;margin:0;',
                                    bodyBorder: false,
                                    border: 0,
                                    autoScroll: true,
                                    defaultType: 'textfield',
                                    autoScroll: false,
                                    fieldDefaults:
                                    {
                                        labelAlign: 'left',
                                        labelWidth: 72,
                                        anchor: '100%',
                                        msgTarget: 'qtip',
                                        autoFitErrors: true
                                    },
                                    items:
                                    [
                                        {
                                            xtype: 'textfield',
                                            name: 'code',
                                            fieldLabel: '单位编号',
                                            emptyText: '最长20个字符',
                                            itemId: 'txtCode',
                                            allowBlank: false,
                                            maxLength: 20
                                        },
                                        {
                                            xtype: 'textfield',
                                            name: 'name',
                                            fieldLabel: '单位名称',
                                            itemId: 'txtName',
                                            emptyText: '最长40个字符',
                                            allowBlank: false,
                                            maxLength: 40
                                        },
                                        {
                                            xtype: 'combobox',
                                            name: 'category',
                                            itemId: 'cboCategory',
                                            fieldLabel: '单位类型',
                                            emptyText: '单位类型',
                                            store: store_master_categories,
                                            displayField: 'Name',
                                            valueField: 'ID',
                                            queryMode: 'local',
                                            hideTrigger: false,
                                            forceSelection: true,
                                            typeAhead: true,
                                            autocomplete: false,
                                            allowBlank: false,
                                            maxLength: 20,
                                            editable: false
                                        }
                                    ],
                                    buttons:
                                    [
                                        {
                                            text: '确定',
                                            height: 26,
                                            margin: 3,
                                            handler: function ()
                                            {
                                                //获得表单对象
                                                var form = this.up('form').getForm();

                                                //获得窗体
                                                var win = this.up('window');

                                                //检查数据是否合法
                                                if (form.isValid())
                                                {
                                                    //提交数据
                                                    form.submit
                                                    (
                                                        {
                                                            url: Common.MakeAbsoluteUrl('Create'),
                                                            method: HttpMethod.Post,
                                                            scope: this.up('window'),
                                                            submitEmptyText: false,
                                                            waitTitle: '请稍候',
                                                            waitMsg: '正在保存数据...',
                                                            params: { parentID: win.current_row.id },
                                                            success: function (form, action)
                                                            {
                                                                //处理提交错误
                                                                if (Common.HandleFormError(form, action))
                                                                {
                                                                    //重置数据
                                                                    form.reset();
                                                                    //隐藏窗体
                                                                    this.hide();
                                                                    //加载数据
                                                                    store.load();
                                                                }
                                                            },
                                                            failure: Common.HandleFormFailure
                                                        }
                                                    );
                                                }
                                            }
                                        },
                                        {
                                            text: '取消',
                                            height: 26,
                                            margin: 3,
                                            handler: function ()
                                            {
                                                //获得表单对象
                                                var form = this.up('form').getForm();
                                                //重置数据
                                                form.reset();
                                                //隐藏窗体
                                                this.up('window').hide();
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                     );
                }

                //设置当前行
                this.win_create.current_row = selection.data;

                //显示窗体
                this.win_create.show();
            }
        },

        //处理编辑按钮点击事件
        onEditClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection()[0];

            //判断是否选中行
            if (selection)
            {
                //判定窗体是否已经创建
                if (!this.win_edit)
                {
                    //创建窗体
                    this.win_edit = new Ext.Window
                    (
                        {
                            title: '编辑组织结构',
                            maximizable: false,
                            maximized: false,
                            width: 360,
                            height: 240,
                            autoScroll: false,
                            bodyBorder: false,
                            draggable: true,
                            isTopContainer: true,
                            modal: true,
                            resizable: false,
                            bodyPadding: 0,
                            closeAction: 'hide',
                            closable: false,
                            layout: 'fit',
                            fieldDefaults:
                            {
                                labelAlign: 'left',
                                labelWidth: 72,
                                anchor: '100%'
                            },
                            listeners:
                            {
                                //挂接显示事件
                                show: function (win, options)
                                {
                                    //查询表单对象对象
                                    var form = win.down('form').getForm();

                                    //加载数据
                                    form.load
                                    (
                                        {
                                            url: Common.MakeAbsoluteUrl('Details?id=' + win.current_row.id),
                                            waitMsg: '数据加载中...',
                                            failure: function (form, action)
                                            {
                                                //处理错误
                                                Common.HandleFormFailure(form, action);

                                                //隐藏窗体
                                                win.hide();
                                            }
                                        }
                                    );
                                }
                            },
                            items:
                            [
                                {
                                    xtype: 'form',
                                    method: HttpMethod.Post,
                                    waitMsgTarget: true,
                                    waitTitle: Common.ApplicationName,
                                    frame: false,
                                    bodyPadding: 12,
                                    bodyStyle: 'background-color:White;margin:0;',
                                    bodyBorder: false,
                                    border: 0,
                                    defaultType: 'textfield',
                                    fieldDefaults:
                                    {
                                        labelAlign: 'left',
                                        labelWidth: 72,
                                        anchor: '100%',
                                        msgTarget: 'qtip',
                                        autoFitErrors: true
                                    },
                                    reader: Ext.create
                                    (
                                        'Ext.data.reader.Json',
                                        {
                                            model: 'TreeNodeData',
                                            record: 'data',
                                            successProperty: 'success',
                                            idProperty: 'id',
                                            messageProperty: 'message'
                                        }
                                    ),
                                    items:
                                    [
                                         {
                                             xtype: 'textfield',
                                             name: 'code',
                                             fieldLabel: '单位编号',
                                             emptyText: '最长20个字符',
                                             itemId: 'txtCode',
                                             allowBlank: false,
                                             maxLength: 20
                                         },
                                        {
                                            xtype: 'textfield',
                                            name: 'name',
                                            fieldLabel: '单位名称',
                                            itemId: 'txtName',
                                            emptyText: '最长40个字符',
                                            allowBlank: false,
                                            maxLength: 40
                                        },
                                        {
                                            xtype: 'combobox',
                                            name: 'category',
                                            itemId: 'cboCategory',
                                            fieldLabel: '单位类型',
                                            emptyText: '单位类型',
                                            store: store_master_categories,
                                            displayField: 'Name',
                                            valueField: 'ID',
                                            queryMode: 'local',
                                            hideTrigger: false,
                                            forceSelection: true,
                                            typeAhead: true,
                                            autocomplete: false,
                                            allowBlank: false,
                                            maxLength: 20,
                                            editable: false
                                        },
                                        {
                                            xtype: 'textfield',
                                            name: 'id',
                                            allowBlank: false,
                                            hidden: true
                                        }
                                    ],
                                    buttons:
                                    [
                                        {
                                            text: '确定',
                                            height: 26,
                                            margin: 3,
                                            handler: function ()
                                            {
                                                //获得表单对象
                                                var form = this.up('form').getForm();

                                                //检查数据是否合法
                                                if (form.isValid())
                                                {
                                                    //提交数据
                                                    form.submit
                                                    (
                                                        {
                                                            url: Common.MakeAbsoluteUrl('Update'),
                                                            scope: this.up('window'),
                                                            submitEmptyText: false,
                                                            waitTitle: '请稍候',
                                                            waitMsg: '正在保存数据...',
                                                            success: function (form, action)
                                                            {
                                                                //处理提交错误
                                                                if (Common.HandleFormError(form, action))
                                                                {
                                                                    //重置数据
                                                                    form.reset();
                                                                    //隐藏窗体
                                                                    this.hide();
                                                                    //刷新数据
                                                                    store.load();
                                                                }
                                                            },
                                                            failure: Common.HandleFormFailure
                                                        }
                                                    );
                                                }
                                            }
                                        },
                                        {
                                            text: '取消',
                                            height: 26,
                                            margin: 3,
                                            handler: function ()
                                            {
                                                //获得表单对象
                                                var form = this.up('form').getForm();

                                                //重置数据
                                                form.reset();

                                                //隐藏窗体
                                                this.up('window').hide();
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    );
                }

                //设置当前行
                this.win_edit.current_row = selection.data;

                //显示窗体
                this.win_edit.show();
            }
        }
    }
);

//挂接环境准备完毕事件
Ext.onReady
(
    function ()
    {
        //初始化快速提示
        Ext.QuickTips.init();

        //设置状态保存提供者（当前设置为Cookie）
        Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider', { path: _Current_ActionUrl }));

        //初始化附加验证类型
        Common.InitAdditionalValidation();

        //创建计量单位存储
        if (!store_master_categories)
        {
            //创建计量单位存储对象
            store_master_categories = Ext.create
            (
                'Ext.data.Store',
                {
                    fields: ['ID', 'Name'],
                    data: master_categories
                }
            );
        }

        //创建数据存储对象
        var store = Ext.create
        (
            'Ext.data.TreeStore',
            {
                autoLoad: true,
                remoteFilter: true,
                remoteSort: true,
                model: 'TreeNodeData',
                defaultRootProperty: 'data',
                defaultRootId: 'parentID',
                folderSort: true,
                clearOnLoad: true,
                url: Common.MakeAbsoluteUrl('Select'),
                proxy:
                {
                    type: 'ajax',
                    url: Common.MakeAbsoluteUrl('Select'),
                    listeners:
                    {
                        exception: function (proxy, response, operation, options)
                        {
                            //处理错误
                            Common.HandleProxyError(proxy, response, operation, options);
                        }
                    }
                }
            }
        );

        Ext.create
        (
            'Tree.Grid',
            {
                store: store,
                stateful: false,
                stateId: 'organization-state-grid',
                height: 600,
                width: '100%',
                id: 'tree-grid',
                title: '',
                singleExpand: false,
                autoScroll: true,
                multiSelect: false,
                lines: true,
                rootVisible: true,
                root:
                {
                    name: '',
                    root: true,
                    code: '组织结构',
                    expanded: true
                },
                collapsible: false,
                useArrows: true,
                renderTo: 'data-grid',
                viewConfig:
                {
                    stripeRows: true,
                    loadingText: '数据加载中...',
                    singleSelect: true
                },
                columns:
                [
                    {
                        xtype: 'treecolumn',
                        text: '单位编号',
                        width: 360,
                        minWidth: 240,
                        sortable: false,
                        dataIndex: 'code'
                    },
                    {
                        text: '单位名称',
                        width: 240,
                        sortable: false,
                        align: 'left',
                        dataIndex: 'name',
                        hideable: false
                    },
                    {
                        text: '单位类型',
                        width: 160,
                        sortable: false,
                        align: 'left',
                        dataIndex: 'categoryName',
                        hideable: false
                    },
                    {
                        text: '',
                        sortable: false,
                        align: 'left',
                        flex: true,
                        hideable: false
                    }
                ]
            }
        );

        $(window).bind
        (
            "resize",
            function ()
            {
                //调整表格宽度
                Common.AdjustGridWidth('tree-grid', 'body_content');
            }
        );

        //调整表格宽度
        Common.AdjustGridWidth('tree-grid', 'body_content');
    }
);
